package _Trash;


import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.lucene.document.Field;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class QuranParser{


	public  ArrayList<org.apache.lucene.document.Document> allVerses = new ArrayList<org.apache.lucene.document.Document>();

	private boolean dbg = false;
	
	public   ArrayList<org.apache.lucene.document.Document>  parse (String fileName){

		try {

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File(fileName)); 		//

			// normalize text representation
			doc.getDocumentElement ().normalize ();
			System.out.println ("Root element of the doc is " + 
					doc.getDocumentElement().getNodeName());

			Element tstmt = doc.getDocumentElement();
			NodeList rootChildren = tstmt.getChildNodes();

			// coverpg, titlepg, bookcoll:
			
			
			NodeList coverpg = tstmt.getElementsByTagName("coverpg");
			NodeList titlepg = tstmt.getElementsByTagName("titlepg");
			NodeList preface = tstmt.getElementsByTagName("preface");
			NodeList suracolls = doc.getElementsByTagName("suracoll");
	
			Node suracoll = suracolls.item(0);
			Element firstSuracoll = (Element)suracoll;	
			
			NodeList suras = firstSuracoll.getElementsByTagName("sura");
			
			int iCounter = 0;

			for (int i = 0 ; i < suras.getLength() ; i++) {
				DBG("suras...");													
				Node currentSura = suras.item(i);

				//bktLong:
				NodeList bktlongs = ((Element)currentSura).getElementsByTagName("bktlong");
				Node firstBktLong = bktlongs.item(0);
				String firstBktLongString = bktlongs.item(0).getTextContent();
				DBG("bktLong "+firstBktLongString);		

		
				//bktShort:
				NodeList bktshort = ((Element)currentSura).getElementsByTagName("bktshort");
				Node firstBktShort = bktshort.item(0);
				String firstBktShortString = bktshort.item(0).getTextContent();
				DBG("\tbktShort"+firstBktShortString);	
				
				

				NodeList epigraphs = ((Element)currentSura).getElementsByTagName("epigraph");
				Node firstEpigraph = epigraphs.item(0);
				String epigraphString;
				if (epigraphs.getLength() == 0)
					epigraphString = "";
				else epigraphString = epigraphs.item(0).getTextContent();
				DBG("epigraph "+ epigraphString);
				

				NodeList verses = ((Element)currentSura).getElementsByTagName("v");
				
				for (int z = 0 ; z < verses.getLength() ; z++) {
					String vContents = verses.item(z).getTextContent(); 				//
					DBG("\t\t\tv" + vContents);
					org.apache.lucene.document.Document currentDocument = new org.apache.lucene.document.Document();
					
					currentDocument.add(new Field("verseContents", vContents, Field.Store.YES, Field.Index.TOKENIZED)); 
					currentDocument.add(new Field("bktlongContents", firstBktLongString, Field.Store.YES, Field.Index.TOKENIZED));
					currentDocument.add(new Field("bktshortContents", firstBktShortString, Field.Store.YES, Field.Index.TOKENIZED));
					currentDocument.add(new Field("bookID",  new Integer(i).toString(), Field.Store.YES, Field.Index.NO));
			        allVerses.add(iCounter,  currentDocument);
			        iCounter++;
				}
			}
		
	}catch (SAXParseException err) {
		System.out.println ("** Parsing error" + ", line " 
				+ err.getLineNumber () + ", uri " + err.getSystemId ());
		System.out.println(" " + err.getMessage ());

	}catch (SAXException e) {
		Exception x = e.getException ();
		((x == null) ? e : x).printStackTrace ();

	}catch (Throwable t) {
		t.printStackTrace ();
	}

	
	return allVerses;

}
			
			public void DBG(String msg) {
				if(dbg) {
					System.out.println("DBG: "+msg);
				}
			}
}